package cn.jingyuan.owl.dao.mybatisplus;

import cn.jingyuan.owl.dao.core.MyBatisPlusCrudDao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.io.Serializable;
import java.util.List;

/**
 * MyBatis-Plus 实现
 *
 * @param <E> 实体
 * @param <ID> 主键
 */
public abstract class AbstractMyBatisPlusCrudDao<E, ID extends Serializable> extends AbstractMyBatisPlusEmptyDao<E, ID>
    implements MyBatisPlusCrudDao<E, ID> {

    @Override
    public int insert(E entity) {
        return getMapper().insert(entity);
    }

    @Override
    public int inserts(List<E> entities) {
        int rows = 0;
        for (E entity : entities) {
            int row = getMapper().insert(entity);
            rows += row;
        }
        return rows;
    }

    @Override
    public int insertSelective(E entity) {

        // fixme
        /*
            获取 mapper class 声明的 E 类型
            获取 E 的 class
            比较 entity class 与 mapper class 是否一致：
                不一致：报错

            获取 mapper class 是否有声明的方法
                无声明：报错
                有声明：执行
         */

        Class<? extends BaseMapper> mClazz = getMapper().getClass();
        Class<?> eClass = entity.getClass();

        String currentMethodName = Thread.currentThread().getStackTrace()[1].getMethodName();  // 获取当前方法名

        // mClazz.getDeclaredAnnotationsByType();

        return 0;
    }

    @Override
    public int insertsSelective(List<E> entities) {
        // fixme
        return 0;
    }

    @Override
    public int deleteById(ID id) {
        return getMapper().deleteById(id);
    }

    @Override
    public int deleteByIds(List<ID> ids) {
        return getMapper().deleteBatchIds(ids);
    }

    @Override
    public int deletesAll() {
        return getMapper().delete(null);
    }

    @Override
    public int updateById(E entity) {
        return getMapper().updateById(entity);
    }

    @Override
    public int updateByIds(List<E> entities) {
        int rows = 0;
        for (E t : entities) {
            int row = getMapper().updateById(t);
            rows += row;
        }
        return rows;
    }

    @Override
    public int updateSelectiveById(E entity) {
        // fixme
        return 0;
    }

    @Override
    public int updateSelectiveByIds(List<E> entities) {
        // fixme
        return 0;
    }

    @Override
    public int update(LambdaUpdateWrapper updateWrapper) {
        return getMapper().update(null, updateWrapper);
    }

    @Override
    public int update(E e, LambdaUpdateWrapper updateWrapper) {
        return getMapper().update(e, updateWrapper);
    }

    @Override
    public E getById(ID id) {
        return getMapper().selectById(id);
    }

    @Override
    public List<E> getByIds(List<ID> ids) {
        return getMapper().selectBatchIds(ids);
    }

    @Override
    public List<E> getAll() {
        return getMapper().selectList(null);
    }

    @Override
    public int count() {
        return getMapper().selectCount(null);
    }

    @Override
    public int countByIds(List<ID> ids) {
        return getMapper().selectBatchIds(ids).size();
    }

    public LambdaQueryWrapper<E> instanceQueryWrapper() {
        return new LambdaQueryWrapper<>();
    }

    public LambdaUpdateWrapper<E> instanceUpdateWrapper() {
        return new LambdaUpdateWrapper<>();
    }

}
